﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package pixelShader {
    import flash.display.*;
    import flash.geom.*;
    import flash.utils.*;
    import Classes.*;
    import plugin.*;
    import flash.filters.*;

    public class AnimatedShaderBitmap extends AnimatedBitmapRoundRobbin {

        public static var s_cached:uint = 0;
        public static var s_total:uint = 0;
        public static var s_frame:uint = 0;
        public static var s_pixelSaved:uint = 0;
        public static var s_pixelUsed:uint = 0;
        private static var s_shadedSpriteSheets:Dictionary = new Dictionary();
        private static var s_textures:Dictionary = new Dictionary();
        private static var s_keys:uint = 0;
        private static var maxZoom:int = Global.gameSettings.getNumber("maxZoom", 4);
        protected static var s_cachedBitmaps:Dictionary = new Dictionary();
        protected static var s_cachedHalfBitmaps:Dictionary = new Dictionary();

        private var m_shaderTexture:BitmapData;
        private var bDisable:Boolean = false;
        protected var m_bitmaps:Array = null;
        protected var m_halfBitmaps:Array = null;
        protected var m_frameSkipCount:int = 0;
        protected var m_useAllFrames:Boolean = false;

        public function AnimatedShaderBitmap(_arg1:BitmapData, _arg2:BitmapData, _arg3:int, _arg4:int, _arg5:int, _arg6:Number){
            super(null, _arg3, _arg4, _arg5, _arg6);
            this.applyTexture(_arg1, _arg2, _arg6);
            this.draw();
        }
        public static function printStats():void{
            var _local1:ConsoleStub = ConsoleStub.getInstance();
            if (_local1 != null){
                _local1.print("Asset", ((((((((("[ShaderAsset] Frames = " + s_frame) + ", Sheets = ") + int(((s_pixelSaved * 4) / 0x0400))) + "KB, Sprites = ") + int(((s_pixelUsed * 4) / 0x0400))) + "KB, Hit = ") + s_total) + "/") + s_cached), 1);
            };
        }

        public function set enable(_arg1:Boolean):void{
            this.bDisable = !(_arg1);
        }
        public function applyTexture(_arg1:BitmapData, _arg2:BitmapData, _arg3:Number):void{
            this.m_shaderTexture = _arg2;
            if (!s_textures[_arg2]){
                s_textures[_arg2] = ++s_keys;
            };
            if (!s_shadedSpriteSheets[_arg1]){
                s_shadedSpriteSheets[_arg1] = ++s_keys;
            };
            var _local4:String = ((("" + s_textures[_arg2]) + "/") + s_shadedSpriteSheets[_arg1]);
            this.buildBitmaps(_arg1, _local4, _arg3);
        }
        private function createSpriteSheet(_arg1:BitmapData):BitmapData{
            var _local2:BitmapData = Processor.instance.doShaderJob(_arg1, this.m_shaderTexture);
            _local2.applyFilter(_local2, _local2.rect, new Point(), new BlurFilter(1.1, 1.1, 2));
            return (_local2);
        }
        public function get texture():BitmapData{
            return (this.m_shaderTexture);
        }
        override public function onUpdate(_arg1:Number):void{
            super.onUpdate(_arg1);
        }
        protected function buildBitmaps(_arg1:BitmapData, _arg2:String, _arg3:Number):void{
            var _local4:BitmapData;
            var _local5:int;
            var _local6:int;
            var _local7:Bitmap;
            var _local8:ColorTransform;
            var _local9:Number;
            var _local10:Number;
            var _local11:Matrix;
            var _local12:BitmapData;
            s_total++;
            this.m_bitmaps = s_cachedBitmaps[_arg2];
            this.m_halfBitmaps = s_cachedHalfBitmaps[_arg2];
            if (((!((this.m_bitmaps == null))) && (!((this.m_bitmaps.length == numFrames))))){
                this.m_bitmaps = null;
            };
            if (this.m_bitmaps == null){
                _local4 = this.createSpriteSheet(_arg1);
                if (this.numFrames < 1){
                    this.m_bitmaps = [];
                    this.m_halfBitmaps = [];
                    return;
                };
                this.m_bitmaps = new Array();
                this.m_halfBitmaps = new Array();
                _local5 = Math.floor((_local4.width / m_frameWidth));
                _local6 = 0;
                while (_local6 < numFrames) {
                    m_frameRect.x = ((_local6 % _local5) * m_frameWidth);
                    m_frameRect.y = (Math.floor((_local6 / _local5)) * m_frameHeight);
                    if ((((_local6 == 0)) && (m_targetBitmapData))){
                        _local12 = m_targetBitmapData;
                    } else {
                        _local12 = new BitmapData(m_frameWidth, m_frameHeight, true, 0);
                    };
                    s_frame++;
                    _local12.copyPixels(_local4, m_frameRect, m_destPoint);
                    this.m_bitmaps.push(_local12);
                    _local6++;
                };
                _local7 = new Bitmap();
                _local8 = null;
                _local9 = Math.floor((m_frameWidth / 2));
                _local10 = Math.floor((m_frameHeight / 2));
                _local11 = new Matrix();
                _local11.scale((_local9 / m_frameWidth), (_local10 / m_frameHeight));
                if ((((_arg3 < 10)) && ((numFrames < 4)))){
                    this.m_useAllFrames = true;
                };
                _local6 = 0;
                while (_local6 < numFrames) {
                    _local12 = new BitmapData((m_frameWidth / 2), (m_frameHeight / 2), true, 0);
                    s_frame++;
                    _local7.bitmapData = this.m_bitmaps[_local6];
                    _local12.draw(_local7, _local11, _local8, null, null, true);
                    this.m_halfBitmaps.push(_local12);
                    if (!this.m_useAllFrames){
                        _local6++;
                    };
                    _local6++;
                };
                s_cachedBitmaps[_arg2] = this.m_bitmaps;
                s_cachedHalfBitmaps[_arg2] = this.m_halfBitmaps;
                s_pixelSaved = (s_pixelSaved + (_local4.width * _local4.height));
                s_pixelUsed = (s_pixelUsed + int((((m_frameWidth * m_frameHeight) * numFrames) * 1.25)));
                s_cached++;
            };
            if (this.m_bitmaps){
                _local12 = this.m_bitmaps[0];
                if (_local12){
                    m_targetBitmapData = _local12;
                    this.bitmapData = _local12;
                    this.scaleX = 1;
                    this.scaleY = 1;
                };
            };
        }
        override protected function draw():void{
            var _local1:BitmapData;
            var _local2:Boolean;
            var _local3:int;
            var _local4:int;
            if (!this.m_shaderTexture){
                return;
            };
            if (this.m_bitmaps == null){
                return;
            };
            if (!this.bDisable){
                this.m_frameSkipCount--;
                if (this.m_frameSkipCount > 0){
                    return;
                };
                this.m_frameSkipCount = 0;
                _local2 = (GlobalEngine.viewport.getZoom() < maxZoom);
                _local3 = Global.world.defCon;
                if (_local3 >= GameWorld.DEFCON_LEVEL2){
                    if (_local3 >= GameWorld.DEFCON_LEVEL3){
                        this.m_frameSkipCount = 4;
                    } else {
                        this.m_frameSkipCount = 2;
                    };
                };
                if (this.m_useAllFrames){
                    this.m_frameSkipCount = 0;
                };
                if (_local2){
                    _local4 = currentFrame;
                    if (!this.m_useAllFrames){
                        _local4 = (_local4 / 2);
                    };
                    _local1 = this.m_halfBitmaps[_local4];
                    if (_local1){
                        m_targetBitmapData = _local1;
                        this.bitmapData = _local1;
                        this.scaleX = 2;
                        this.scaleY = 2;
                    };
                    return;
                };
            };
            _local1 = this.m_bitmaps[currentFrame];
            if (_local1){
                m_targetBitmapData = _local1;
                this.bitmapData = _local1;
                this.scaleX = 1;
                this.scaleY = 1;
            };
        }

    }
}//package pixelShader 
